package com.xdtech.play.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.DateUtil;
import com.xdtech.common.utils.DateUtil.DateStyle;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.play.dao.PlayTradeRecordDao;
import com.xdtech.play.model.PlayMember;
import com.xdtech.play.model.PlayTradeRecord;
import com.xdtech.play.service.PlayMemberService;
import com.xdtech.play.service.PlayTradeRecordService;
import com.xdtech.play.util.PlayContants;
import com.xdtech.play.vo.PlayMemberItem;
import com.xdtech.play.vo.PlayTradeRecordItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2016-05-08 23:42:57
 * @since 1.0
 * @see
 */
@Service
public class PlayTradeRecordServiceImpl implements PlayTradeRecordService {
	private Log log = LogFactory.getLog(PlayTradeRecordServiceImpl.class);
	@Autowired
	private PlayTradeRecordDao playTradeRecordDao;
	@Autowired
	private BaseDao baseDao;
	
	@Autowired
	private PlayMemberService playMemberService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param entity
	 */
	public void save(PlayTradeRecord entity) {
		playTradeRecordDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<PlayTradeRecord> entities) {
		playTradeRecordDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param entity
	 */
	public void delete(PlayTradeRecord entity) {
		playTradeRecordDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		playTradeRecordDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param id
	 * @return
	 */
	public PlayTradeRecord get(Long id) {
		return playTradeRecordDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @return
	 */
	public List<PlayTradeRecord> getAll() {
		return playTradeRecordDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> playTradeRecords = null;
		long rows = 0;
		if (pg!=null) {
			Page<PlayTradeRecord> page = playTradeRecordDao.findPage(pg,"from PlayTradeRecord order by createTime desc", values);
			playTradeRecords = BeanUtils.copyListProperties(
					PlayTradeRecordItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<PlayTradeRecord> playTradeRecordList = playTradeRecordDao.find("from PlayTradeRecord order by id desc", values);
			playTradeRecords = BeanUtils.copyListProperties(
					PlayTradeRecordItem.class, playTradeRecordList);
			rows = playTradeRecords.size();
		}
		results.put("total", rows);
		results.put("rows", playTradeRecords);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdatePlayTradeRecord(PlayTradeRecordItem item) {
		PlayTradeRecord playTradeRecord = null;
		if (item.getId()==null) {
			playTradeRecord = new PlayTradeRecord();
		}else {
			playTradeRecord = playTradeRecordDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(playTradeRecord, item);
		playTradeRecordDao.save(playTradeRecord);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param playTradeRecordId
	 * @return
	 */
	public PlayTradeRecordItem loadPlayTradeRecordItem(Long playTradeRecordId) {
		PlayTradeRecord playTradeRecord = playTradeRecordDao.get(playTradeRecordId);
		return PlayTradeRecordItem.createItem(playTradeRecord);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deletePlayTradeRecordInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deletePlayTradeRecordInfo(List<Long> playTradeRecordIds) {
		for (Long id : playTradeRecordIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:57
	 * @modified by
	 * @return
	 */
	@Override
	public List<PlayTradeRecordItem> loadItems() {
		List<PlayTradeRecord> playTradeRecords = getAll();
		return PlayTradeRecordItem.createItems(playTradeRecords);
	}
	@Override
	public ResultMessage inPlayAccount(PlayTradeRecordItem item) {
		PlayMember playMember = playMemberService.get(item.getMemberId());
		ResultMessage rm = new ResultMessage();
		if (playMember!=null) {
			PlayTradeRecord playTradeRecord = new PlayTradeRecord();
			playTradeRecord.setAccount(item.getAccount());
			playTradeRecord.setPayType(item.getPayType());
			playTradeRecord.setTradeType(PlayContants.TRADE_IN);
			playTradeRecord.setPlayMember(playMember);
			playTradeRecord.setOperateTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			playTradeRecord.setOperator(item.getOperator());
			//设置玩家账户金额
			int account = playTradeRecord.getAccount()==null?0:playTradeRecord.getAccount();
			int accountBlance = playMember.getMoney()+account;
			playTradeRecord.setAccountBlance(accountBlance);
			playTradeRecordDao.save(playTradeRecord);
			playMember.setMoney(accountBlance);
			
			playMemberService.save(playMember);
			
			if (playMember.getMoney()<0) {
				rm.setMsg("充值后，账户余额还是小于零，请继续充值.");
			}
		}else {
			rm = new ResultMessage(false, "玩家账户不存在");
		}
		return rm;
	}
	@Override
	public ResultMessage outPlayAccount(PlayTradeRecordItem item) {
		PlayMember playMember = playMemberService.get(item.getMemberId());
		ResultMessage rm = new ResultMessage();
		if (playMember!=null) {
			PlayTradeRecord playTradeRecord = new PlayTradeRecord();
			playTradeRecord.setAccount(-item.getAccount());
			playTradeRecord.setPayType(item.getPayType());
			playTradeRecord.setTradeType(PlayContants.TRADE_OUT);
			playTradeRecord.setPlayMember(playMember);
			playTradeRecord.setOperateTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			playTradeRecord.setOperator(item.getOperator());
			//设置玩家账户金额
			int account = playTradeRecord.getAccount()==null?0:playTradeRecord.getAccount();
			int accountBlance = playMember.getMoney()+account;
			if (accountBlance<0) {
				rm.setFailMsg("提取金额超出账户余额，无法操作");
			}else {
				playTradeRecord.setAccountBlance(accountBlance);
				playTradeRecordDao.save(playTradeRecord);
				playMember.setMoney(accountBlance);
				playMemberService.save(playMember);
			}
			
			
		}else {
			rm = new ResultMessage(false, "玩家账户不存在");
		}
		return rm;
	}
	@Override
	public void createPlayStartRecord(PlayMember member, Integer account,String operator) {
		PlayTradeRecord playTradeRecord = new PlayTradeRecord();
		playTradeRecord.setAccount(-account);
		playTradeRecord.setPayType(PlayContants.PAY_KZ);
		playTradeRecord.setTradeType(PlayContants.TRADE_KZ);
		playTradeRecord.setPlayMember(member);
		playTradeRecord.setOperateTime(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		playTradeRecord.setOperator(operator);
		//设置玩家账户金额
		int accountBlance = member.getMoney() - account;
		playTradeRecord.setAccountBlance(accountBlance);
		playTradeRecordDao.save(playTradeRecord);
		member.setMoney(accountBlance);
		playMemberService.save(member);
	}

}
